﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Operations
{
	internal class NewCircle : Operation
	{
        Engine.Selection Selected;
        Engine.Selection ItemAdded;
		Vector2 Position;
        Data.EntityRecord Entity;

        internal NewCircle(Engine.Selection Selected, Vector2 Position)
		{
			this.Selected = Selected;
			this.Position = Position;
		}

		internal override ReturnCode Do(EditorState ES)
		{
            Entity = ES.GetTopObject(Selected) as Data.EntityRecord;
            if (Entity == null) return ReturnCode.DoNotAddToUndoStack;

            Data.Circle Circle = new Data.Circle();
            Circle.Position = Position - Entity.GetPosition();

			Entity.AttachedElements.Add(Circle);

            ItemAdded = new Engine.Selection(Engine.Selection.Types.EntityElement, Selected.TopIndex,
				(ES.Map.Elements[Selected.TopIndex] as Data.EntityRecord).AttachedElements.Count - 1, 0);
            ES.RebuildNode(ItemAdded.TopIndex);

			ES.SelectItem(ItemAdded);

			return ReturnCode.AddToUndoStack;
		}

		internal override void Undo(EditorState ES)
		{
			Entity.AttachedElements.RemoveAt(ItemAdded.MiddleIndex);
            ES.RebuildNode(ItemAdded.TopIndex);

            ES.SelectItem(new Engine.Selection(Engine.Selection.Types.MapElement, ItemAdded.TopIndex, 0, 0));
		}
	}
}
